//
//  ZJKVisitorView.swift
//  MicroBlog
//
//  Created by VAD on 16/4/16.
//  Copyright © 2016年 朱江坤. All rights reserved.
/**
App Key：2127509851
App Secret：59eaec342829199493334a714817d0b3
*/


import UIKit

import SnapKit

//定义代理协议
@objc protocol ZJKVisitorViewDelegate: NSObjectProtocol{
    
    //定义代理登录方法
    func loginButtonClick()
    
    //定义代理注册方法
    func registerButtonClick()
    
}

class ZJKVisitorView: UIView {
    
    //设置代理属性
    weak var delegate: ZJKVisitorViewDelegate?
    
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        
        fatalError("init(coder:) has not been implemented")
    }
    
    func setVisitorInfo(imgNamed: String?,message: String?){
        
        if let img = imgNamed,msg = message{
            
            iconView.image = UIImage(named: img)
            
            messageLabel.text = msg
        }else{
            
            startAnim()
        }
    }
    
    
    private func startAnim(){
        //显示出来
        circleView.hidden = false
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * M_PI
        
        anim.duration = 20
        
        anim.repeatCount = MAXFLOAT
        
        anim.removedOnCompletion = false
        
        circleView.layer.addAnimation(anim, forKey: nil)
        
    }
    
    @objc private func loginButtonClick(){
        
        //实现代理的登录方法
        delegate?.loginButtonClick()
    }
    
    @objc private func registerButtonClick(){
        
        //实现代理的注册方法
        delegate?.registerButtonClick()
    }
    //布局子控件
    private func setupUI(){
        
        backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        
        addSubview(circleView)
        addSubview(maskIconView)
        addSubview(iconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
        // 圆圈
        circleView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(iconView)
        }
        
        // 消息label
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView)
            make.top.equalTo(circleView.snp_bottom).offset(15)
        }
        //注册按钮
        registerButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(messageLabel.snp_leading)
            make.top.equalTo(messageLabel.snp_bottom).offset(15)
            make.width.equalTo(100)
        }
        
        // 登录按钮
        loginButton.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(messageLabel)
            make.top.equalTo(registerButton)
            make.width.equalTo(100)
        }
        
        // 透明度
        maskIconView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleView)
        }
        
    }

    
    
        //懒加载
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //转圈的view
    private lazy var circleView: UIImageView = {
        
        let imageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        
        imageView.hidden = true
        
        return imageView
    }()
    
    private lazy var messageLabel: UILabel = {
        
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 14, preferredMaxLayoutWidth: 230)
        
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        
        label.textAlignment = .Center
        
        return label
    }()
    
    // 注册按钮
    private lazy var registerButton: UIButton = {
        
        let button = UIButton(textColor: UIColor.orangeColor(), fontSize: 14)
        
        button.addTarget(self, action: "registerButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        button.setTitle("注册", forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "common_button_white"), forState: UIControlState.Normal)
        
        return button
    }()
    
    // 登录按钮
    private lazy var loginButton: UIButton = {
        
        let button = UIButton(textColor: UIColor.darkGrayColor(), fontSize: 14)
        
        button.addTarget(self, action: "loginButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        button.setTitle("登录", forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "common_button_white"), forState: UIControlState.Normal)
        
        return button
    }()
    
    // 半透明背景
    private lazy var maskIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))


}
